//
//  UserPartOrderView.m
//  SmallElephant
//
//  Created by Faith on 2024/11/10.
//

#import "UserPartOrderView.h"
#import "../Order/UserOrderController.h"

@interface UserPartOrderView() <UICollectionViewDelegate, UICollectionViewDataSource, PHSegmentTabbarDelegate>

/// 标题视图
@property (nonatomic, strong) UIView *titleView;

/// 分段 Tabbar 视图
@property (nonatomic, strong) PHSegmentTabbar *segmentTabbar;

/// 按钮列表视图
@property (nonatomic, strong) UICollectionView *buttonContentView;

/// 按钮数据源
@property (nonatomic, strong) NSArray *buttonDataList;

/// 待付款列表容器视图
@property (nonatomic, strong) UIView *waitPayOrderContainerView;

/// 待付款列表视图
@property (nonatomic, strong) UICollectionView *waitPayOrderView;

/// 待付款数据源
@property (nonatomic, strong) NSArray *waitPayOrderDataList;

@end



@implementation UserPartOrderView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupViews];
    }
    return self;
}


/// 设置视图
- (void)setupViews {
    // 设置自身样式
    [self applyStyle:@"display: flex; flex-direction: column; background: #FFFFFF; border-radius: 10px; margin-top: 17px; margin-left: 10px; margin-right: 10px; padding: 15px; padding-top: 10px;"];
    // 添加标题
    [self addSubview:self.titleView style:@"display: flex; flex-direction: row; margin-bottom: 10px;"];
    // 添加按钮列表
    [self addSubview:self.buttonContentView style:@"display: flex;"];
    // 待付款订单列表
    [self addSubview:self.waitPayOrderView style:@"display: flex; background: #F5F5F5; border-radius: 8px; height:70px;"];
}

- (UIView *)titleView {
    if(!_titleView) {
        _titleView = [[UIView alloc] init];
        
        // TAB切换控件
        _segmentTabbar = [[PHSegmentTabbar alloc] init];
        _segmentTabbar.delegate = self;
        [_titleView addSubview:_segmentTabbar style:@"display: flex; flex: 1;"];
        
        // 查看全部按钮
        UIButton *btnOrderAll = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnOrderAll setTitle:@"全部订单 >" forState:UIControlStateNormal];
        [_titleView addSubview:btnOrderAll style:@"display: flex; color: #999999; font-size: 13px; align-self: center; margin-left: 10px;"];
    }
    return _titleView;
}

/**
 * 懒加载按钮容器
 */
- (UICollectionView *)buttonContentView {
    if(!_buttonContentView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 0; // 每列之间的最小间距
        layout.minimumLineSpacing = 0; // 每行之间的最小间距
        layout.sectionInset = UIEdgeInsetsMake(0, 0, SIZE(10), 0); // 上下左右间距
        layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
        _buttonContentView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _buttonContentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _buttonContentView.backgroundColor = [UIColor whiteColor];
        _buttonContentView.delegate = self;
        _buttonContentView.dataSource = self;
        [_buttonContentView registerClass:[UserPartOrderButtonCell class] forCellWithReuseIdentifier:@"kUserPartOrderButtonCell"];
        [_buttonContentView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
        
        
    }
    return _buttonContentView;
}

/**
 * 懒加载订单容器
 */
- (UICollectionView *)waitPayOrderView {
    if(!_waitPayOrderView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 0; // 每列之间的最小间距
        layout.minimumLineSpacing = 0; // 每行之间的最小间距
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); // 上下左右间距
        layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
        _waitPayOrderView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _waitPayOrderView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _waitPayOrderView.showsHorizontalScrollIndicator = NO;
        _waitPayOrderView.backgroundColor = [UIColor whiteColor];
        _waitPayOrderView.delegate = self;
        _waitPayOrderView.dataSource = self;
        _waitPayOrderView.pagingEnabled = YES;
        [_waitPayOrderView registerClass:[UserPartOrderWaitPayCell class] forCellWithReuseIdentifier:@"kUserPartOrderWaitPayCell"];
    }
    return _waitPayOrderView;
}

/// 单元格数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if(collectionView==self.buttonContentView) {
        return self.buttonDataList.count;
    } else if(collectionView==self.waitPayOrderView) {
        return self.waitPayOrderDataList.count;
    }
    return 0;
}

/**
 * 列表单元视图
 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if(collectionView==self.buttonContentView) {
        UserPartOrderButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kUserPartOrderButtonCell" forIndexPath:indexPath];
        [cell setData:self.buttonDataList[indexPath.row]];
        return cell;
    } else if(collectionView==self.waitPayOrderView) {
        UserPartOrderWaitPayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kUserPartOrderWaitPayCell" forIndexPath:indexPath];
        [cell setData:self.waitPayOrderDataList[indexPath.row]];
        return cell;
    }
    return nil;
}

/// 单元格点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if(collectionView==self.buttonContentView) {
        UserOrderController *controller = [[UserOrderController alloc] init];
        [[self getRootController] pushViewController:controller];
    }
}

/**
 * 监听属性变化
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"contentSize"]) {
        if(object==self.buttonContentView) {
            CGFloat height = self.buttonContentView.contentSize.height;
            self.buttonContentView.yoga.height = YGPointValue(height);
           
        }
    }
}
    


/// 设置数据
- (void)setData:(NSDictionary*)data {
    [self.segmentTabbar setData:@[
        @"商城订单",
        @"我的预约单"
    ]];
    
    self.buttonDataList = @[
        @{
            @"icon": @"icon-order-wait-pay@3x",
            @"title": @"待付款",
        },
        @{
            @"icon": @"icon-order-wait-audit@3x",
            @"title": @"待复核",
        },
        @{
            @"icon": @"icon-order-wait-confirm@3x",
            @"title": @"待收货",
        },
        @{
            @"icon": @"icon-order-confirmed@3x",
            @"title": @"已签收",
        },
        @{
            @"icon": @"icon-order-reback@3x",
            @"title": @"退款/售后",
        }
    ];
    if(!data) {
        self.waitPayOrderDataList = @[
            @{
                @"cover": @"icon-avatar@3x",
                @"status": @"等待付款",
                @"remain": @"10:23:22"
            },
            @{
                @"cover": @"icon-avatar@3x",
                @"status": @"等待付款",
                @"remain": @"20:23:22"
            },
            @{
                @"cover": @"icon-avatar@3x",
                @"status": @"等待付款",
                @"remain": @"5:23:22"
            }
        ];
        [self.waitPayOrderView reloadData];
        [self.waitPayOrderView applyStyle:@"display: flex;"];
    } else {
        self.waitPayOrderDataList = @[];
        [self.waitPayOrderView reloadData];
        [self.waitPayOrderView applyStyle:@"display: none;"];
    }
    
}

- (void)didSelectIndex:(NSInteger)index {
    NSLog(@"选择了: %ld", index);
}


@end





@interface UserPartOrderButtonCell()

@property (nonatomic, strong) UIImageView *iconView;

@property (nonatomic, strong) UILabel *textView;

@end

@implementation UserPartOrderButtonCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    _iconView = [[UIImageView alloc] init];
    [self.contentView addSubview:_iconView style:@"width: 36px; height: 36px; align-self: center;"];
    _textView = [[UILabel alloc] init];
    [self.contentView addSubview:_textView style:@"width: 100%; margin-top: 5px; font-size: 13px; color: #333333; text-align: center;"];
    
}

- (CGSize)sizeThatFits:(CGSize)size {
    [self.contentView configureLayoutWithBlock:^(YGLayout * _Nonnull layout) {
        layout.isEnabled = true;
        layout.flexDirection = YGFlexDirectionColumn;
        layout.width = YGPointValue((SCREEN_WIDTH - SIZE(60)) / 5);
    }];
    [self.contentView.yoga applyLayoutPreservingOrigin:YES dimensionFlexibility:YGDimensionFlexibilityFlexibleHeight];
    return [self.contentView sizeThatFits:size];
}

- (void)setData:(NSDictionary *)data {
    [self.iconView setImage:[UIImage imageWithName:data[@"icon"]]];
    self.textView.text = data[@"title"];
}


@end



/// 待付款单元格实现
@interface UserPartOrderWaitPayCell()

/// 待付款商品图片
@property (nonatomic, strong) UIImageView *coverView;

/// 订单状态视图
@property (nonatomic, strong) UIView *infoView;

/// 状态文字
@property (nonatomic, strong) UILabel *statusView;

/// 剩余时间
@property (nonatomic, strong) UILabel *remainTimeView;

/// 去支付按钮
@property (nonatomic, strong) UIButton *btnPay;

@end

@implementation UserPartOrderWaitPayCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    
    _coverView = [[UIImageView alloc] init];
    [self.contentView addSubview:_coverView style:@"display: flex; width: 45px; height: 45px; border-radius: 5px; align-self: center; margin-top:10px; margin-left: 10px; margin-bottom: 10px;"];
    
    _infoView = [[UIView alloc] init];
    
    _statusView = [[UILabel alloc] init];
    [_infoView addSubview:_statusView style:@"font-size: 14px; color: #333333;"];
    
    _remainTimeView = [[UILabel alloc] init];
    [_infoView addSubview:_remainTimeView style:@"margin-top: 5px; font-size: 13px; color: #666666;"];
    
    [self.contentView addSubview:_infoView style:@"display: flex; flex: 1; flex-direction: column; margin-left: 5px; align-self: center;"];
    
    _btnPay = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnPay setTitle:@"去支付" forState:UIControlStateNormal];
    [self.contentView addSubview:_btnPay style:@"display: flex; width: 70px; height: 30px; margin-left: 5px; margin-right: 17px; border-radius: 15px; font-size: 14px; color: #EF2020; align-self: center; border-width: 1px; border-color: #EF2020;"];
    
}

- (CGSize)sizeThatFits:(CGSize)size {
    [self.contentView configureLayoutWithBlock:^(YGLayout * _Nonnull layout) {
        layout.isEnabled = true;
        layout.flexDirection = YGFlexDirectionRow;
        layout.width = YGPointValue(SCREEN_WIDTH - SIZE(49));
    }];
    [self.contentView.yoga applyLayoutPreservingOrigin:YES dimensionFlexibility:YGDimensionFlexibilityFlexibleHeight];
    return [self.contentView sizeThatFits:size];
}

- (void)setData:(NSDictionary *)data {
    [self.coverView setImageUrl:@"https://wx3.sinaimg.cn/orj360/d6d6719bly1hvfv3fiup1j20j60c5goj.jpg"];
    self.statusView.text = data[@"status"];
    self.remainTimeView.text = [NSString stringWithFormat:@"剩余时间: %@", data[@"remain"]];
}

@end
